<!DOCTYPE html>
<html class="x-admin-sm">
<head>
    <meta charset="UTF-8">
    <title>欢迎页面-X-admin2.2</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
    <!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
    <!--[if lt IE 9]>-->
    <!--<script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>-->
    <!--<script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>-->
    <script type="text/javascript" src="../../common/js/public.js"></script>
    <script type="text/javascript" src="../../common/js/publicUtils.js"></script>
</head>
<body>
<div class="layui-fluid" id="flowDiv" onload="onLoad()">
    <div class="zTreeBackground left" style="float: left;width:300px">
        <div class="titlediv">
            <p style="text-align:right;background-color:white;line-height: 30px">
                组织管理<i class="layui-icon layui-icon-refresh" style="margin-left: 36%" onclick="refreshRoot()"></i>&ensp;&ensp;
            </p>

        </div>
        <div class="searchdiv" >
            <div class="layui-input-inline" style="width:100%">
                <input type="text" id="search" name="keyword" autocomplete="off" class="layui-input" placeholder="请输入名称"/>
                <i class="layui-icon layui-icon-search" style="position: absolute;top:8px;right: 8px;" onclick="searchByName()"></i>
            </div>
        </div>
        <div class="ztreediv" id="ztreediv">
            <div class="ztreeuldiv">
                <ul id="treeStructure" class="ztree"></ul>
            </div>
        </div>
    </div>
    <div class="zTreeBackground right" style="float: left; padding-left:1%;width:70%">
        <table class="layui-hide" id="datatable"></table>
        <script type="text/html" id="toolbar">
        </script>

    </div>
</div>
<div id='gpsmap' style="display: none;height: 100%;">
    <div id="allmap" style="overflow:hidden;zoom:1;position:relative;width: 100%; height:100%;">
        <div id="map" style="height:100%;-webkit-transition: all 0.5s ease-in-out;transition: all 0.5s ease-in-out;"></div>
    </div>
</div>
</body>
<script type="text/javascript">
    var zTreeObj,form,tableObj,table,element,projectid;//点击当前项数据id;
    $(function(){
        loadbutton("toolbar");//加载按钮loadbutton(id)，id为button容器id，不传默认为buttongroup
        //loadTree();
        initTree("#treeStructure");
    })
    window.onload = getContentSize;
    window.onresize = getContentSize;

    var setting = {
        check: {
            enable: false,
            chkboxType: { "Y": "s", "N": "s" }
        },
        edit:{
            enable: false
        },
        async: {
            enable: true,
            url: publicUrl+"orgstation/getProjectsForTree",
            headers: {"Authorization":getData("Authorization")},
            dataFilter: formatedata,
            otherParam:{//传入查询参数
                //注意，要传入动态变化的参数必须用return的方式
                "personId":"3"
            },
            autoParam: ["id","type"]
        },
        data: {
            simpleData: {
                enable: true,
                idKey:"id",
                pIdKey:"pid",
                isParent:"parent",
                rootPid:"",
                type:"type"
            },
            key: {
                isParent:"parent",
                url: "xUrl",
                icon:"xIcon",
                level:"orgLevel",
                name:"shortName"
            }
        },
        callback: {
            beforeClick: beforeClick,
            onClick: zTreeOnClick
        }
    };

    /**
     * 页面自适应
     */
    function getContentSize() {
        var wh = document.documentElement.clientHeight;
        var eh = 30;
        ch = (wh - eh) + "px";
        document.getElementById( "flowDiv" ).style.height = ch;
        document.getElementById( "ztreediv" ).style.height = (wh - eh - 50)+"px";
        document.body.style.height = (wh)+"px";
        $(".zTreeBackground.right").css("width",($(document).width()-350)+'px');//自适应表格处理
    }

    /**
     * @Author bxf
     * @Description 搜索框按钮
     * @Date 11:20 2019/7/17
     * @Param
     * @return
     **/
    function searchByName(){
        var search = $("#search").val();
        if(!search){
            return ;
        }
        loadTree("#treeStructure");
    }
    /**
     * @Author bxf
     * @Description 刷新左边树
     * @Date 11:20 2019/7/17
     * @Param
     * @return
     **/
    function refreshRoot(){
        $("#search").val("");
        initTree("#treeStructure");
    }

    //zTreeObj = $.fn.zTree.init($("#treeDemo"), setting, zNodes);
    function formatedata(treeId,parentNode,responseData) {
        return changeIcon(formatTreeParseData(treeId,parentNode,responseData,null));
    }

    function initTree(id){
        $(id).html('');
        publicAjax (null,'company/getByPersonId',{"personId":"3"},false,null,function(data){
            zTreeObj = $.fn.zTree.init($(id),setting, changeIcon(data.data));
        })
    }

    function loadTree(id){
        $(id).html('');
        publicAjax (null,'orgstation/getProjectsForTree',{"personId":"3","name":$("#search").val(),"stationFlag":1,"type":9},false,null,function(data){
            zTreeObj = $.fn.zTree.init($(id),setting, changeIcon(data.data));
            zTreeObj.expandAll(true);
        })
    }

    // zTree 的参数配置，深入使用请参考 API 文档（setting 配置详解）

    function beforeClick(treeId, treeNode, clickFlag) {
        if(treeNode.type!=5){
            layer.msg("请选择项目！",{time:500});
            return false;
        }
    }


    function zTreeOnClick(event, treeId, treeNode, clickFlag) {
        nodeSelected = treeNode;
        parentNode = zTreeObj.getNodeByParam("id",treeNode.parentId,null);
        if(treeNode.type===5){
            projectid = treeNode.id;
            loadTable();
        }

    }

    function setFontCss_ztree(treeId, treeNode) {
        if (treeNode.id == 0) {
            //根节点
            return {color: "#333", "font-weight": "500"};
        } else if (treeNode.isParent == false) {
            //叶子节点
            return (!!treeNode.highlight) ? {color: "#ff0000", "font-weight": "bold"} : {
                color: "#660099",
                "font-weight": "normal"
            };
        } else {
            //父节点
            return (!!treeNode.highlight) ? {color: "#ff0000", "font-weight": "bold"} : {
                color: "#333",
                "font-weight": "normal"
            };
        }
    }


    function loadTable(){
        var width = $(".left-nav").css("width");
        $(".zTreeBackground.right").css("width",($(document).width()-380)+'px');//自适应表格处理

        var mycode = $("#code").val();
        var myname = $("#name").val();
        var myclassy = $("#classy").val();
        var myelementName = $("#elementName").val();
        var tableurl = publicUrl+'orgstation/selectListByPid';
        layui.use(['form','table','element'], function(){
            element = layui.element;
            form = layui.form;
            table = layui.table;
            tableObj = table.render({
                elem: '#datatable',
                size: 'sm',
                toolbar: '#toolbar',
                even: true, //开启隔行背景
                height: 'full-50',
                headers: {"Authorization":getData("Authorization")},
                method: 'post',
                url: tableurl, //stationId='+window.parent.window.parentId,//8AE7DECA-8E41-4FAB-8B30-9FD1B0B7FA33,78A9E5D8-20FB-4436-BBE8-A25EAC38D056
                where:{
                    "pid":projectid
                },
                response: {
                    statusCode: 2000,
                },
                parseData: function(res){ //res 即为原始返回的数据
                    return formatTableParseData(res);
                },
                request: {
                    pageName: 'pageIndex' //页码的参数名称，默认：page
                    ,limitName: 'pageSize' //每页数据量的参数名，默认：limit
                },
                cols: [[
                    {
                        type:'checkbox',
                        width: "5px",
                    },
                    {
                        type: 'numbers',
                        title: '序号',
                        width: "5%",
                        align: 'center',
                    },{
                        field: 'name',
                        title: '工点名称',
                        sort: true,
                        width: "18%",
                        align: 'center',
                    }, {
                        field: 'infName',
                        title: '工点类型',
                        sort: true,
                        width: "10%",
                        align: 'center'
                    },{
                        field: 'beginMileage',
                        title: '起点里程',
                        sort: true,
                        width: "10%",
                        align: 'center'
                    },{
                        field: 'endMileage',
                        title: '终点里程',
                        sort: true,
                        width: "12%",
                        align: 'center'
                    },{
                        field: 'gps',
                        title: 'GPS坐标',
                        width: "14%",
                        align: 'center',
                    },{
                        field: 'sort',
                        title: '排序',
                        width: "5%",
                        align: 'center',
                    }, {
                        field: 'profile',
                        title: '工点概况',
                        width: "20%",
                        align: 'center',
                    }]],
                page: true
            });
            element.render();
        });
    }
    /**
     * 编辑项目工点
     */
    function editOrgstation(){
        var obj = table.checkStatus('datatable');
        if(!obj || obj.data.length!=1){
            layer.alert("请选择一项信息进行修改！");
            return false;
        }
        var data = obj.data[0];
        var nodes = zTreeObj.getSelectedNodes()[0];
        data.pname = nodes.name;
        data.category = nodes.category;
        var iframeWin ;
        layer.open({
            type: 2,
            area: ["650px" , "650px"],
            fix: false,
            //不固定
            maxmin: false,
            shade: 0.3,
            title: "编辑工点",
            content: "./addorgstation.html",
            btn: ['确定', '关闭'],
            // 弹层外区域关闭
            shadeClose: false,
            yes: function(index, layero) {
                var resultdata = iframeWin.contentWindow.updateOrgstation();
                if(resultdata && resultdata.code==2000){
                    loadTable();
                    refreshSelectNode(zTreeObj,nodes.id);
                    layer.closeAll();
                }else{
                    return false;
                }
            },
            cancel: function(index) {
                return true;
            },
            success: function(layero, index){
                iframeWin = layero.find('iframe')[0];
                iframeWin.contentWindow.inintData(data,2);//修改按钮
            }
        });
    }
    /**
     * @Author bxf
     * @Description 添加项目工点
     * @Date 9:51 2019/7/18
     * @Param
     * @return
     **/
    function addOrgstation(){
        var nodes = zTreeObj.getSelectedNodes()[0];
        if(nodes.type!=5){
            layself("请选择项目");
            return false;
        }
        var iframeWin ;
        layer.open({
            type: 2,
            area: ["650px" , "650px"],
            fix: false,
            //不固定
            maxmin: false,
            shade: 0.3,
            title: "新增工点",
            content: "./addorgstation.html",
            btn: ['应用','确定','关闭'],
            // 弹层外区域关闭
            shadeClose: false,
            yes: function(index, layero) {
                var resultdata = iframeWin.contentWindow.addOrgstation();//应用
                if(resultdata && resultdata.code==2000){
                    loadTable();
                    refreshSelectNode(zTreeObj,nodes.id);
                }
                return false;
                //layer.closeAll();
            },
            btn2: function(index, layero){
                //按钮【按钮二】的回调
                var resultdata = iframeWin.contentWindow.addOrgstation();//应用
                if(resultdata && resultdata.code==2000){
                    loadTable();
                    refreshSelectNode(zTreeObj,nodes.id);
                    setTimeout(function () {
                        layer.closeAll();
                    },1000);
                }else{
                    return false;
                }

                //return false 开启该代码可禁止点击该按钮关闭
            },
            btn3: function(index, layero){
                //按钮【按钮三】的回调

            },
            cancel: function(index) {
                return true;
            },
            success: function(layero, index){
                iframeWin = layero.find('iframe')[0];
                iframeWin.contentWindow.inintData(nodes,1);//添加按钮
            }
        });
    }

    /* *
     * @Author bxf
     * @Description 删除记录
     * @Date 11:19 2019/7/23
     * @param null
     * @return
     */
    function  deleteOrgstation() {
        //获取选中项
        var obj = table.checkStatus('datatable');
        if(!obj || obj.data.length<1){
            layer.alert("请至少选择一项信息！");
            return false;
        }
        var ids = new Array();
        for(var i=0;i<obj.data.length;i++){
            ids.push(obj.data[i].id);
        }
        var nodes = zTreeObj.getSelectedNodes()[0];
        layui.use(['layer'],function(){
            var layer = layui.layer;
            layer.confirm('确定删除？', {
                btn: ['确定', '取消'] //可以无限个按钮
            }, function(){
                var delurl = "orgstation/deleteOrgStation";
                var data = {"ids":ids.join(",")};//删除
                publicAjax (null,delurl,data,null,null,function (r) {
                    if(r.code==2000){
                        laysuccess("删除成功");
                        loadTable();
                        refreshSelectNode(zTreeObj,obj.data[0].projectId);
                    }else{
                        layerror("删除失败")
                    }
                })
            }, function(){
                layer.closeAll();

            });
        })
    }


    function uploadOrgstation(){
        var nodes = zTreeObj.getSelectedNodes()[0];
        if(nodes.type!=5){
            layself("请选择项目");
            return false;
        }
        var iframeWin ;
        layer.open({
            type: 2,
            area: ["500px" , "350px"],
            fix: false,
            //不固定
            maxmin: false,
            shade: 0.3,
            title: "导入工点",
            content: "./uploadorgstation.html",
            btn: ['关闭'],
            // 弹层外区域关闭
            shadeClose: false,
            yes: false,
            cancel: function(index) {
                return true;
            },
            success: function(layero, index){
                iframeWin = layero.find('iframe')[0];
                iframeWin.contentWindow.inintOrgstation(nodes);//初始化按钮
            }
        });
    }

    function moveOrgstation(){
        //获取选中项
        var obj = table.checkStatus('datatable');
        if(!obj || obj.data.length<1){
            layer.alert("请至少选择一项信息！");
            return false;
        }
        var ids = new Array();
        for(var i=0;i<obj.data.length;i++){
            ids.push(obj.data[i].id);
        }
        var nodes = zTreeObj.getSelectedNodes()[0];
        var iframeWin ;
        layer.open({
            type: 2,
            area: ["430px" , "600px"],
            fix: false,
            //不固定
            maxmin: false,
            shade: 0.3,
            title: "移动工点",
            content: "./moveOrgstation.html",
            btn: ['确定','关闭'],
            // 弹层外区域关闭
            shadeClose: false,
            yes: function(index, layero) {
                var resultdata = iframeWin.contentWindow.submit();//应用
                if(resultdata && resultdata.code==2000){
                    loadTable();
                    refreshSelectNode(zTreeObj,nodes.id);
                    setTimeout(function () {
                        layer.closeAll();
                    },1000);
                }
                return false;
                //layer.closeAll();
            },
            cancel: function(index) {
                return true;
            },
            success: function(layero, index){
                iframeWin = layero.find('iframe')[0];
                iframeWin.contentWindow.inintOrgstation(ids);//初始化按钮
            }
        });
    }

    /**
     * 打开选中指定节点
     * @param treeobj
     * @param id
     */
    function getAppointNode(treeobj,id){
        if(id){
            var node = treeobj.getNodeByParam("id",id);
            treeobj.selectNode(node,true);//指定选中ID的节点
            //treeobj.expandNode(node, true, false);//指定选中ID节点展开
        }5
    }



    /**
     * 刷新指定节点
     */
    function refreshSelectNode(obj,id) {
        var node = obj.getNodeByParam("id",id);
        obj.reAsyncChildNodes(node,"refresh");
    }
    /**
     * 选中某个节点
     */
    function pointNode(obj,id){
        var nodeClick = obj.getNodeByParam("id", id);
        obj.selectNode(nodeClick);//选择点
        obj.setting.callback.onClick(null, id, nodeClick);//调用点击事件
    }






</script>
</html>